package com.client.koshercook.client;

import java.util.Iterator;
import java.util.LinkedList;
import com.google.gwt.user.client.rpc.IsSerializable;

/** Server will get the ingredients using the iterator */
public class ClientRequest implements IsSerializable, Iterable<String>{
	
	private LinkedList<String> myIngs; //ingredients in search 
	public LinkedList<String> mustIngs;	//ingredients that must appear in answer
	public LinkedList<String> forbiddenSites; //sites the search will skip. sitesList = {"כושר-קוק", "כיפה", "השף הלבן","רוטב"};

	
	private int cookTimeLimit; //in minutes, -1 is for no limit
	private boolean onlyDairy;
	private boolean onlyMeat;
	private boolean onlyParve;
	private int page;
	private String type;
	
	/*construct a request to the server*/
	public ClientRequest() {
		myIngs = new LinkedList<String>();
		mustIngs = new LinkedList<String>();
		forbiddenSites = new LinkedList<String>();
		type = "הכל";
		page = 0;
		cookTimeLimit = -1;
		onlyDairy = false;
		onlyMeat = false;
		onlyParve = false;
	}
	
	public String toString(){
		return "<page = " + page + "> <myIngs size = " + myIngs.size() + ">";
	}
	
	public void setIngredientMust(String ingName, boolean set){
		if(set==false){								//if want to remove it
			mustIngs.remove(ingName);
		}else if(!mustIngs.contains(ingName)){		//if want to add it
			mustIngs.add(ingName);
		}
	}
	
	public LinkedList<String> getIng(){
		return myIngs;
	}
	
	public void addIng(String ingName){
		myIngs.add(ingName);
	}
	
	public void deleteIng(String ingName){
		myIngs.remove(ingName);
		mustIngs.remove(ingName);
	}
	
	public void clearIngs(){
		myIngs.clear();
		mustIngs.clear();
	}
	
	public int ingsIndexOf(String ingName){
		return myIngs.indexOf(ingName);
	}
	
	public boolean isIngsEmpty(){
		return myIngs.isEmpty();
	}
	
	public boolean ingsContains(String ingName){
		return myIngs.contains(ingName);
	}
	
	public void setPage(int page){
		this.page = page;
	}
	
	public int getPage(){
		return page;
	}
	
	public void setKosherDairy(){
		onlyDairy = true;
		onlyMeat = onlyParve = false;
	}
	
	public boolean isKosherDairy(){
		return onlyDairy;
	}
	
	public void setKosherMeat(){
		onlyMeat = true;
		onlyDairy = onlyParve = false;
	}
	
	public boolean isKosherMeat(){
		return onlyMeat;
	}
	
	public void setKosherParve(){
		onlyParve = true;
		onlyMeat = onlyDairy = false;
	}
	
	public boolean isKosherParve(){
		return onlyParve;
	}
	
	public void setKosherAll(){
		onlyMeat = onlyDairy = onlyParve = false;
	}
	
	public boolean isKosherAll(){
		return !onlyMeat && !onlyDairy && !onlyParve; 
	}
	
	public Iterator<String> iterator() {
		return new myIterator();
	}
	
	/** use this to iterate on the myIngs list */
	private class myIterator implements Iterator<String>{

		Iterator<String> innerIterator;
		
		private myIterator(){
			innerIterator = myIngs.iterator();
		}
		
		public boolean hasNext() {
			return innerIterator.hasNext();
		}

		public String next() {
			return innerIterator.next();
		}

		public void remove() {
			innerIterator.remove();
		}
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public String getType() {
		return type;
	}
	
	public void addForbiddenSite(String site) {
		forbiddenSites.add(site);
	}
	
	public boolean removeSite(String site) {
		return forbiddenSites.remove(site);
	}

	public void setCookTimeLimit(int time) {
		cookTimeLimit = time;
	}
	
	public int getCookTimeLimit() {
		return cookTimeLimit;
	}
	
	public void clearFilters() {
		forbiddenSites.clear();
		cookTimeLimit = -1;
	}
}
